package com.mjx.jdk.io;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Created by meijingxiang on 2017/4/5.
 */
public class Zip {

    public static void main(String[] args) {
//        zipByIO("/Users/macyf0004/Desktop/Spring源码深度解析.pdf");
        zipByNIO("/Users/macyf0004/Desktop/Spring源码深度解析.pdf");
    }

    private static void zipByIO(String filePath) {
        ZipOutputStream zipOutputStream = null;
        InputStream inputStream = null;
        File file = new File(filePath);
        String zipPath = getZipPath(filePath);
        File zipFile = new File(zipPath);
        try {
            if (!zipFile.exists()){
                zipFile.createNewFile();
            }
            zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile));
            inputStream = new FileInputStream(file);
            zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
            zipOutputStream.setComment("test");
            byte[] bytes = new byte[1024];
            while (inputStream.read(bytes) > -1){
                zipOutputStream.write(bytes);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                inputStream.close();
                zipOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void zipByNIO(String filePath){
        ZipOutputStream zipOutputStream = null;
        FileInputStream inputStream = null;
        File file = new File(filePath);
        String zipPath = getZipPath(filePath);
        File zipFile = new File(zipPath);
        FileChannel fileChannel = null;
        try {
            if (!zipFile.exists()){
                zipFile.createNewFile();
            }
            zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile));
            inputStream = new FileInputStream(file);
            fileChannel = inputStream.getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
            zipOutputStream.setComment("test");
            while (fileChannel.read(byteBuffer) > -1){
                byteBuffer.flip();
               zipOutputStream.write(byteBuffer.array());
                byteBuffer.clear();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                inputStream.close();
                zipOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static String getZipPath(String filePath){
        return filePath.substring(0, filePath.lastIndexOf(".")) + ".zip";
    }
}
